

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup
from xmw.tianya.bot import *

from xmw import tianya

from xmw.tianya import error_tianya
import os
import urllib2, urllib
import uuid

from google.appengine.ext import webapp



url = "http://cache.tianya.cn/techforum/content/516/39685.shtml"
url = "http://www.tianya.cn/techforum/content/516/72019.shtml"
class MainPage(webapp.RequestHandler):
    def get(self):
        #link :http://www.tianya.cn/publicforum/content/free/1/1752945.shtml
        
        page = urllib2.urlopen(url)
        soup = BeautifulSoup(page, fromEncoding="GB18030")
        #print soup.prettify()
        page.close()
        
        #get_last_post_date(soup)
        #return 
      
        
        """
        self.response.headers['Content-Type'] = 'text/plain'
        #self.response.out.write('OK')
        self.response.out.write(get_page_type(soup))
        self.response.out.write("<BR>")
        
        self.response.out.write(get_form_cnt(soup))
        self.response.out.write("<BR>")
        
        self.response.out.write(get_md5(get_rs_strTitle_aa(soup)))
        self.response.out.write("\n")
        
        
        self.response.out.write(get_intLogo(soup))
        self.response.out.write("<BR>")
        
        self.response.out.write(get_rs_permission(soup))
        self.response.out.write("<BR>")
        
        self.response.out.write(get_apn(soup))
        self.response.out.write("<BR>")
        """
        #self.response.out.write(get_title(soup))
        #self.response.out.write("<BR>")
        #cnt=get_form_cnt(soup)
        #self.response.out.write(type(cnt))
        
        soup = walk_to_last_form(soup,url)
        #self.response.out.write(repr(get_title(soup)))
        #self.response.out.write(soup.prettify('GB18030'))
        l = []
        l.append(url)
        #print "XXXXXXXXXXXXXXXXXXXXX"
        thread = tianya.Thread(title=repr(get_title(soup)), \
                             thread_id="0", \
                             page_url=l, \
                             last_scan=datetime.datetime.today(), \
                             latest_post_date=get_last_post_date(soup), \
                             type=get_page_type(soup), \
                             page_cnt=get_form_cnt(soup), \
                             chrAuthor=get_chrAuthor(soup),\
                             status="OK"
                             
                             )
        #print thread.latest_post_date
        #print thread
        """
        thread.thread_id="0"
        thread.created=now()
        l=[]
        l.append(url)
        thread.page_url(l)
        thread.last_scan=thread.created
        thread.latest_post_date=now()   ##
        
        thread.type=get_page_type(soup)
        thread.pages_cnt=get_form_cnt(soup)
        thread.status="OK"
        """
        template_values = {
                           "thread":thread
                           }     
        path = os.path.join(os.path.dirname(__file__), 'template/thread.html')
        #self.response.out.write(path)
        self.response.out.write(template.render(path, template_values))
        #self.response.out.write("KKKKKKKKKKKKKK")
        
        
        
        
        
        

def main():
    application = webapp.WSGIApplication(
                                       [('/get_thread', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
